<?php

namespace App\Providers;

use App\Http\Models\Topic;
use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    // 使用的时候才开始注册服务
    protected $defer = false;

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // 后启动

        // 设置string类型默认长度
        Schema::defaultStringLength(191);

        /**
         * 通过获取[driver]的原型, 来注册自定义的[driver]及设置
         */

        // 设置faker默认语言
        $this->app->singleton(\Faker\Generator::class, function () {
            return \Faker\Factory::create('zh_TW');
        });

        // 设置 Carbon 的语言
        Carbon::setLocale('zh');

        // 公共视图渲染
        View::composer('layout.sidebar', function ($view){
            /* @var \Illuminate\View\View $view */
            $topics = Topic::all();
            $view->with('topics', $topics);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // 先注册
    }
}
